<?php
include("include/config.inc.php");
$pageName = 'outGoingEntry';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$plannArray          = 0;
	$outGoingChallan     = 0;
  $selPartyId          = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
	$unprocessincomingId = isset($_REQUEST['unprocessincomingId']) && ($_REQUEST['unprocessincomingId'] > 0) ? $_REQUEST['unprocessincomingId']: 0;
  if((isset($_POST['submitBtn']) || isset($_POST['printBtn'])) && ($_POST['submitBtn'] > 0 || $_POST['printBtn'] > 0))
  {
  	$date                = $_POST['dateYear']."-".$_POST['dateMonth']."-".$_POST['dateDay'];
  	$outGoingChallan = $_POST['outChallanNo'];
  	foreach($_POST['selectedIncomingId'] as $key => $value)
  	{
//      $selMaxChallan = "SELECT MAX(outChallenNo) + 1 AS outChallanNo
//		                      FROM outgoingmaster";
//	    $selMaxChallanRes = mysql_query($selMaxChallan);
//	    if($maxRow = mysql_fetch_array($selMaxChallanRes));
//	    {
//	      if($maxRow['outChallanNo'] == NULL)
//	      	$outGoingChallan = 1;
//	    	else
//	    	  $outGoingChallan = $maxRow['outChallanNo'];
//	    }
      
     	$insertOutgoing = "INSERT INTO outgoingmaster(date, incomingId, planningId, outChallenNo, finalProcessId, incomingQuantity,
  		                                              dispatchQuantity, remainQuantity, incomingWeight, dispatchWeight,
  		                                              remainWeight)
  		                          VALUES ('".$date."',
  		                                  ".$value.",
  		                                  '".$_POST['planningId']."', 
  		                                  ".$outGoingChallan.",
  		                                  ".$_POST['finalProcessId'][$key].",
  		                                  ".$_POST['quantity'][$key].",
  		                                  ".$_POST['dispatchQuantity'][$key].",
  		                                  ".$_POST['remainingQuantity'][$key].",
  		                                  ".$_POST['weight'][$key].",
  		                                  ".$_POST['dispatchWeight'][$key].",
  		                                  ".$_POST['remainingWeight'][$key].")";
      $insertOutgoingRes = mysql_query($insertOutgoing);
      if($insertOutgoingRes)
      {
      	$selInc = "SELECT qty, weight, outQuantity, outWeight FROM incoming WHERE incomingId = ".$value;
      	$selIncRes = mysql_query($selInc);
      	if($selRow = mysql_fetch_array($selIncRes))
      	{
      		$outQuantity = $selRow['outQuantity'] + $_POST['dispatchQuantity'][$key];
      		$outWeight   = $selRow['outWeight'] + $_POST['dispatchWeight'][$key];
      		
      		if($selRow['qty'] == $outQuantity && $selRow['weight'] == $outWeight)
      		{
      	    $setQuery = " status = 'outgoingDone', outgoingDone = '1', outQuantity = ".$outQuantity.", outWeight   = ".$outWeight." ";
      	  }
      	  else
      	  {
      	    $setQuery = " outQuantity = ".$outQuantity.", outWeight   = ".$outWeight." ";
      	  }
      		
        	$updateIncoming = "UPDATE incoming 
        	                      SET ".$setQuery.",
        	                          unprocessReturnNote = '".$_POST['unprocessReturnNote']."'
        	                    WHERE incomingId = ".$value;
          $updateIncomingRes = mysql_query($updateIncoming);
      	}
      }
  	}
  	if($_POST['printBtn'] > 0)
  	{
  		?>
	    <script type="text/javascript">
	      window.open("shaktiPdf1.php?outChallenNo="+<?php echo $outGoingChallan ?>);
	    </script>  		 
	    <?php
  	  //header("Location:shaktiPdf1.php?outChallenNo=".$outGoingChallan);
  	}
  	else
  	  header("Location:outGoingList.php");
  	  exit();
  }
  
  if(isset($_REQUEST['partyId']) && $_REQUEST['partyId'] > 0)
  {
	  $selIncoming = "SELECT incomingId, partyName, challan, pro1.processName AS proName1,
	                         pro2.processName AS proName2, pro3.processName AS proName3, outQuantity, outWeight
	                    FROM incoming
	                    JOIN party ON incoming.partyId = party.partyId
	                    JOIN process AS pro1 ON incoming.processId1 = pro1.processId
	                    LEFT JOIN process AS pro2 ON incoming.processId2 = pro2.processId
	                    LEFT JOIN process AS pro3 ON incoming.processId3 = pro3.processId
	                   WHERE outgoingDone = '0'
	                     AND incoming.partyId = ".$_REQUEST['partyId'];
	  $selIncomingRes = mysql_query($selIncoming);
	  $plannArray = array();
	  $count = 0;
	  while($incRow = mysql_fetch_array($selIncomingRes))
	  {
		  $plannArray[$count]['incomingId'] = $incRow['incomingId'];
		  $plannArray[$count]['challan']    = $incRow['challan'];
		  $plannArray[$count]['partyName']  = $incRow['partyName'];
		  $plannArray[$count]['proName1']   = $incRow['proName1'];
		  $plannArray[$count]['proName2']   = $incRow['proName2'];
		  $plannArray[$count]['proName3']   = $incRow['proName3'];
		  $plannArray[$count]['planningId'] = "";
		  $plannArray[$count]['quantity']   = 0;
		  $plannArray[$count]['weight']     = 0;
		  $selPlanning = "SELECT planningId,quantity,weight
		                    FROM planning
		                   WHERE planning.status = 'outgoing' 
		                     AND planning.incomingId = ".$incRow['incomingId'];
		  $selPlanningRes = mysql_query($selPlanning);
		  while($planRow = mysql_fetch_array($selPlanningRes))
		  {
		  	$plannArray[$count]['planningId'] .= $planRow['planningId'].",";
		    $plannArray[$count]['quantity']  += $planRow['quantity'];
		    $plannArray[$count]['weight']    += $planRow['weight'];
		  }
		  $plannArray[$count]['planningId'] = substr($plannArray[$count]['planningId'], 0, -1);
		  $plannArray[$count]['quantity'] = $plannArray[$count]['quantity'] - $incRow['outQuantity'];
		  $plannArray[$count]['weight'] = $plannArray[$count]['weight'] - $incRow['outWeight'];
		  $count++;
	  }
	  
	  $selMaxChallan = "SELECT MAX(outChallenNo) + 1 AS outChallanNo
	                      FROM outgoingmaster";
    $selMaxChallanRes = mysql_query($selMaxChallan);
    if($maxRow = mysql_fetch_array($selMaxChallanRes));
    {
      if($maxRow['outChallanNo'] == NULL)
    	  $outGoingChallan = 1;
    	else
    	  $outGoingChallan = $maxRow['outChallanNo'];
    }
  }
  $k = 0;
  $catTitleArray = array();
  $select = "SELECT partyId,partyName
               FROM party
              WHERE partyId IN (SELECT partyId FROM planning WHERE status = 'outgoing' OR status = 'directOut')
              ORDER BY partyName";
  $selectResult = mysql_query($select);
  while($row = mysql_fetch_array($selectResult))
  {
    $catTitleArray['partyId'][$k]   = $row['partyId'];
    $catTitleArray['partyName'][$k] = $row['partyName'];
    $k++;
  }

  $fpArray = array();
  $fp = 0;
  $selectFP = "SELECT finalProcessId,finalProcessName
                 FROM finalprocess
                ORDER BY finalProcessName";
  $selectFPResult = mysql_query($selectFP);
  while($fpRow = mysql_fetch_array($selectFPResult))
  {
    $fpArray['finalProcessId'][$fp]   = $fpRow['finalProcessId'];
    $fpArray['finalProcessName'][$fp] = $fpRow['finalProcessName'];
    $fp++;
  }

  include("./bottom.php");
  $smarty->assign("msg",'<font size="6">No Record Found</font>');
	$smarty->assign("plannArray",$plannArray);
	$smarty->assign("unprocessincomingId",$unprocessincomingId);
  $smarty->assign("selPartyId",$selPartyId);
  $smarty->assign("outGoingChallan",$outGoingChallan);
  $smarty->assign("catTitleArray",$catTitleArray);
	$smarty->assign("fpArray",$fpArray);
	$smarty->display("outGoingEntry.tpl");
}
?>